// 建立扫描存储装饰器信息list
import {paramType,httpMethod,parseType,routerInterface, controllerInterface,paramInterface,parseInterface} from "./decoratorInterface"

export const routerList: routerInterface[] = []
export const controllerList: controllerInterface[] = []
export const parseList: parseInterface[] = []
export const paramList: paramInterface[] = []

// 路由类信息存储
export function Router(baseName: string = ''): Function {
    return (constructor: any) => {
        routerList.push({
            constructor,
            baseName
        })
    }
}

// 路由类里面的请求方法 get/post + 子级路由
function Method(type: httpMethod): Function {
    return (path: string) => (target: any, name: string, descriptor: PropertyDescriptor) => {
        controllerList.push({
            target,
            type,
            path,
            method: name,
            controller: descriptor.value
        })
    }
}

// 格式化参数装饰器
export function Parse(type: parseType):Function {
    return (target: any, name: string, index: number) => {
        parseList.push({
            target,
            type,
            method: name,
            index
        })
    }
}

// 注入请求参数
function Param(position:paramType):Function {
    return (key?: string) => (target: any, name: string, index: number) => {
        paramList.push({
            target,
            key,
            position,
            method: name,
            index
        })
    }
}

export const Body = Param("body")
export const Header = Param("header")
export const Cookie = Param("cookie")
export const Query = Param("query")
export const Get = Method("get")
export const Post = Method("post")


